home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / info-service / gopher / Unix / xgopher.1.3 / resources.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-05-25  |  13.0 KB  |  421 lines

  1. /* resources.c
  2.    process application resources(options) using the X resource manager. */
  3.  
  4.      /*---------------------------------------------------------------*/
  5.      /* Xgopher        version 1.3     08 April 1993                  */
  6.      /*                version 1.2     20 November 1992               */
  7.      /*                version 1.1     20 April 1992                  */
  8.      /*                version 1.0     04 March 1992                  */
  9.      /* X window system client for the University of Minnesota        */
  10.      /*                                Internet Gopher System.        */
  11.      /* Allan Tuchman, University of Illinois at Urbana-Champaign     */
  12.      /*                Computing and Communications Services Office   */
  13.      /* Copyright 1992, 1993 by                                       */
  14.      /*           the Board of Trustees of the University of Illinois */
  15.      /* Permission is granted to freely copy and redistribute this    */
  16.      /* software with the copyright notice intact.                    */
  17.      /*---------------------------------------------------------------*/
  18.  
  19.  
  20. #include <stdio.h>
  21.  
  22. #include <X11/Xlib.h>
  23. #include <X11/StringDefs.h>
  24. #include <X11/Intrinsic.h>
  25.  
  26.  
  27. #include "osdep.h"
  28.  
  29. #include "conf.h"
  30. #include "globals.h"
  31. #include "resources.h"
  32. #include "typeres.h"
  33. #include "popres.h"
  34. #include "listP.h"
  35. #include "misc.h"
  36. #include "panel.h"
  37.  
  38. /* Note on boolean data types.
  39.    Xlib.h    defines Bool, True, False (as int, 1, 0)
  40.    Intrinsic.h    defines Boolean, TRUE, FALSE (as typedef char, 1, 0)
  41.  
  42.    To keep some modules of this program independent of X, the new
  43.    type BOOLEAN is introduced (typedef int), and constants TRUE and
  44.    FALSE are conditionally defined in conf.h to be 1 and 0.
  45.    An implicit conversion from Boolean (resources) to BOOLEAN
  46.    is assumed. */
  47.  
  48.  
  49. static    char    *initPrintCmd, *initImageCmd, *initTelnetCmd,
  50.         *initTn3270Cmd, *initBkFile;
  51. static    Boolean        initAppendBk, initLoadBk, initResetOptions;
  52. static    showLevel    initShowItems;
  53.  
  54. static    Widget    topLevel;
  55.  
  56. /* saveChangableResources
  57.    save initial value of resources that may be changed during execution */
  58.  
  59. void
  60. saveChangableResources(arP)
  61. gopherAppResourcesP    arP;
  62. {
  63.     char    *temp;
  64.  
  65.     initBkFile =
  66.         (char *) malloc(sizeof(char) * (strlen(arP->bookmarkFile)+1));
  67.     strcpy(initBkFile, arP->bookmarkFile);
  68.  
  69.     initPrintCmd =
  70.         (char *) malloc(sizeof(char) * (strlen(arP->printCommand)+1));
  71.     strcpy(initPrintCmd, arP->printCommand);
  72.  
  73.     initImageCmd =
  74.         (char *) malloc(sizeof(char) * (strlen(arP->imageCommand)+1));
  75.     strcpy(initImageCmd, arP->imageCommand);
  76.  
  77.     initTelnetCmd =
  78.         (char *) malloc(sizeof(char) * (strlen(arP->telnetCommand)+1));
  79.     strcpy(initTelnetCmd, arP->telnetCommand);
  80.  
  81.     initTn3270Cmd =
  82.         (char *) malloc(sizeof(char) * (strlen(arP->tn3270Command)+1));
  83.     strcpy(initTn3270Cmd, arP->tn3270Command);
  84.  
  85.     initAppendBk    = arP->appendBookmarks;
  86.     initShowItems    = arP->showItems;
  87.     initLoadBk    = arP->loadBookmarks;
  88.     initResetOptions= arP->resetOptions;
  89. }
  90.  
  91.  
  92. /* restoreChangableResources
  93.    restore initial value of resources that may have been changed
  94.    during execution */
  95.  
  96. void
  97. restoreChangableResources(arP)
  98. gopherAppResourcesP    arP;
  99. {
  100.     arP->bookmarkFile    = initBkFile;
  101.     arP->printCommand    = initPrintCmd;
  102.     arP->imageCommand    = initImageCmd;
  103.     arP->telnetCommand    = initTelnetCmd;
  104.     arP->tn3270Command    = initTn3270Cmd;
  105.  
  106.     arP->appendBookmarks    = initAppendBk;
  107.     arP->showItems        = initShowItems;
  108.     arP->loadBookmarks    = initLoadBk;
  109.     arP->resetOptions    = initResetOptions;
  110. }
  111.  
  112.  
  113. #define DEF_PREFIX    "< ? >"
  114. #define DEF_DESCR    "type ? item"
  115. #define DEF_DESCR_LEN    32
  116.  
  117. /* getTypeResources
  118.    get resources that define an extended type */
  119.  
  120. typeResources *
  121. getTypeResources(name, class)
  122. char    *name, *class;
  123. {
  124.     static typeResources    typeRes;
  125.     static char        defPrefix[PREFIX_LEN+1];
  126.     static char        defDescr[DEF_DESCR_LEN];
  127.     char            t;
  128.     char            *p;
  129.  
  130.     /* assume last character of name is the type letter */
  131.     /* substitute this letter in place of the '?' in defaults */
  132.  
  133.     t = name[strlen(name)-1];
  134.  
  135.     strncpy(defPrefix, DEF_PREFIX, PREFIX_LEN);
  136.     p = index(defPrefix, '?');
  137.     if (p != (char *) NULL) *p = t;
  138.  
  139.     strncpy(defDescr, DEF_DESCR, DEF_DESCR_LEN);
  140.     p = index(defDescr, '?');
  141.     if (p != (char *) NULL) *p = t;
  142.  
  143.     typeEntry[TE_DESCR].default_addr = defDescr;
  144.     typeEntry[TE_PREFIX].default_addr = defPrefix;
  145.     XtGetSubresources (topLevel, (XtPointer) &typeRes,
  146.                 (String) name, (String) class,
  147.                 typeEntry, XtNumber(typeEntry), NULL, 0); 
  148.  
  149.     return (&typeRes);
  150. }
  151.  
  152.  
  153. /* getPopupPosResources
  154.    get resources that define popup positioning */
  155.  
  156. popupPosResources *
  157. getPopupPosResources(name, class, defaults)
  158. char            *name, *class;
  159. popupPosResources    *defaults;
  160. {
  161.     static popupPosResources popupPosRes;
  162.  
  163.     /* each popup may request its own defaults.  Set them now. */
  164.  
  165.     popupPosEntry[PE_FROM].default_addr =
  166.                 (XtPointer) defaults->positionFrom;
  167.     popupPosEntry[PE_X].default_addr = 
  168.                 (XtPointer) defaults->xPosition;
  169.     popupPosEntry[PE_Y].default_addr = 
  170.                 (XtPointer) defaults->yPosition;
  171.     popupPosEntry[PE_HJUST].default_addr = 
  172.                 (XtPointer) defaults->horizontalJustification;
  173.     popupPosEntry[PE_VJUST].default_addr = 
  174.                 (XtPointer) defaults->verticalJustification;
  175.     popupPosEntry[PE_XPERCENT].default_addr = 
  176.                 (XtPointer) ((int) defaults->xPercent);
  177.     popupPosEntry[PE_YPERCENT].default_addr = 
  178.                 (XtPointer) ((int) defaults->yPercent);
  179.  
  180.     XtGetSubresources (topLevel, (XtPointer) &popupPosRes,
  181.                 (String) name, (String) class,
  182.                 popupPosEntry, XtNumber(popupPosEntry),
  183.                 NULL, 0); 
  184.  
  185.     return (&popupPosRes);
  186. }
  187.  
  188.  
  189. /* getApplicationResources
  190.    get application resources */
  191.  
  192. gopherAppResources *
  193. getApplicationResources(top, argc, argv)
  194. Widget    top;
  195. int    argc;
  196. char    **argv;
  197. {
  198.     static gopherAppResources    appResources;
  199.  
  200.     topLevel = top;
  201.  
  202.     /* get application resources */
  203.  
  204.     XtGetApplicationResources(topLevel, (XtPointer) &appResources,
  205.             resources, XtNumber(resources), NULL, 0);
  206.  
  207.     if (! appResources.defaultsInstalled) return NULL;
  208.  
  209.     /* in public mode, apply restrictions such as are desirable
  210.        for a public login to Xgopher.  */
  211.     
  212.     if (appResources.publicMode) {
  213.         appResources.allowSave        = False;
  214.         appResources.allowPrint        = False;
  215.         appResources.allowTelnet    = False;
  216.         appResources.allowTn3270    = False;
  217.         appResources.allowBookmarkSave    = False;
  218.         appResources.allowCopy        = False;
  219.         appResources.appendBookmarks    = True;
  220.         appResources.loadBookmarks    = True;
  221.         appResources.markRoot        = True;
  222.         appResources.resetOptions    = True;
  223.         appResources.optionsButton    = False;
  224.         appResources.singleItemButton    = False;
  225.         appResources.showItems        = showAvailable;
  226.  
  227.  
  228.         /* these are other resources that the installer
  229.            may wish to set in the resources file, but
  230.            they should not be forced here.
  231.  
  232.             appResources.allowImage        = False;
  233.             appResources.hasSound        = False;
  234.  
  235.             appResources.warpCursor        = True;
  236.             appResources.doubleClick    = False;
  237.             appResources.concurrentText    = 1;
  238.             appResources.allowHold        = False;
  239.             appResources.allowFtp        = False;
  240.             appResources.bookmarkFile    = <filename>;
  241.  
  242.             the next two should go as a pair:
  243.             appResources.restartButton    = False;
  244.             appResources.swapRestartAndQuit    = True;
  245.  
  246.            Also, be sure that any remaining commands (imageCommand,
  247.            soundCommand, etc.) have no shell-escapes or other
  248.            ways to do damage.
  249.         */
  250.     }
  251.  
  252.     itemStart     = appResources.itemStart;
  253.     itemIncrement = appResources.itemIncrement;
  254.     dirStart      = appResources.dirStart;
  255.     dirIncrement  = appResources.dirIncrement;
  256.  
  257.     setHelpFile (appResources.helpFile);
  258.     setBkmkFile (appResources.bookmarkFile);
  259.     setBkmkAppend (appResources.appendBookmarks);
  260.  
  261.     infoDirLabel = XtMalloc(
  262.         sizeof (char) * strlen(appResources.infoDirLabel) + 1);
  263.     markDirLabel = XtMalloc(
  264.         sizeof (char) * strlen(appResources.markDirLabel) + 1);
  265.     strcpy(infoDirLabel, appResources.infoDirLabel);
  266.     strcpy(markDirLabel, appResources.markDirLabel);
  267.  
  268.     if (appResources.logFile != NULL  &&  *appResources.logFile != NULLC) {
  269.  
  270.         /* use open/fdopen instead of just fopen so the
  271.            mode can be set for privacy in the log file */
  272.  
  273.         int    logFD;
  274.  
  275.         if ((logFD = open (tildePath(appResources.logFile),
  276.                   O_WRONLY | O_CREAT, LOG_FILE_MODE)) < 0) {
  277.             perror ("open");
  278.             fprintf (stderr, "cannot open log file \'%s\'\n",
  279.                     tildePath(appResources.logFile));
  280.             logFP = NULL;
  281.         } else if ((logFP = fdopen(logFD, "w")) == NULL) {
  282.             fprintf (stderr, "cannot open log file \'%s\'\n",
  283.                     tildePath(appResources.logFile));
  284.         } else {
  285.             setbuf(logFP, NULL);
  286.         }
  287.     } else {
  288.         logFP = NULL;
  289.     }
  290.  
  291.     saveChangableResources(&appResources);
  292.  
  293.  
  294. #ifdef DEBUG
  295.     LOG (logFP, "Application Resources\n");
  296.     LOG (logFP, "\tdefaultsInstalled:\t%s\n",
  297.             appResources.defaultsInstalled ? "True" : "False");
  298.     LOG (logFP, "\tresourcesVersion:\t%s\n",
  299.             appResources.resourcesVersion);
  300.     LOG (logFP, "\trootServer:\t%s\n",
  301.             appResources.rootServer);
  302.     LOG (logFP, "\trootPort:\t%d\n",
  303.             appResources.rootPort);
  304.     LOG (logFP, "\trootPath:\t%s\n",
  305.             appResources.rootPath);
  306.     LOG (logFP, "\tmainTitle:\t%s\n",
  307.             appResources.mainTitle);
  308.     LOG (logFP, "\tallowSave:\t%s\n",
  309.             appResources.allowSave ? "True" : "False");
  310.     LOG (logFP, "\tallowPrint:\t%s\n",
  311.             appResources.allowPrint ? "True" : "False");
  312.     LOG (logFP, "\tprintCommand:\t%s\n",
  313.             appResources.printCommand);
  314.     LOG (logFP, "\thelpFile:\t%s\n",
  315.             appResources.helpFile);
  316.     LOG (logFP, "\tdirectoryTime:\t%d\n",
  317.              appResources.directoryTime);
  318.     LOG (logFP, "\tallowImage:\t%s\n",
  319.             appResources.allowImage ? "True" : "False");
  320.     LOG (logFP, "\timageCommand:\t%s\n",
  321.             appResources.imageCommand);
  322.     LOG (logFP, "\thasSound:\t%s\n",
  323.             appResources.hasSound ? "True" : "False");
  324.     LOG (logFP, "\tsoundCommand:\t%s\n",
  325.             appResources.soundCommand);
  326.     LOG (logFP, "\tbookmarkFile:\t%s\n",
  327.             appResources.bookmarkFile);
  328.     LOG (logFP, "\tallowBookmarkSave:\t%s\n",
  329.             appResources.allowBookmarkSave ? "True" : "False");
  330.     LOG (logFP, "\tappendBookmarks:\t%s\n",
  331.             appResources.appendBookmarks ? "True" : "False");
  332.     LOG (logFP, "\tallowTelnet:\t%s\n",
  333.             appResources.allowTelnet ? "True" : "False");
  334.     LOG (logFP, "\ttelnetCommand:\t%s\n",
  335.             appResources.telnetCommand);
  336.     LOG (logFP, "\tallowTn3270:\t%s\n",
  337.             appResources.allowTn3270 ? "True" : "False");
  338.     LOG (logFP, "\ttn3270Command:\t%s\n",
  339.             appResources.tn3270Command);
  340.     LOG (logFP, "\tloadBookmarks:\t%s\n",
  341.             appResources.loadBookmarks ? "True" : "False");
  342.     LOG (logFP, "\trestartButton:\t%s\n",
  343.             appResources.restartButton ? "True" : "False");
  344.     LOG (logFP, "\tswapRestartAndQuit:\t%s\n",
  345.             appResources.swapRestartAndQuit ? "True" : "False");
  346.     LOG (logFP, "\toptionsButton:\t%s\n",
  347.             appResources.optionsButton ? "True" : "False");
  348.     LOG (logFP, "\tsingleItemButton:\t%s\n",
  349.             appResources.singleItemButton ? "True" : "False");
  350.     LOG (logFP, "\tshowItems:\t%d\n",
  351.             appResources.showItems);
  352.     LOG (logFP, "\tallowFtp:\t%s\n",
  353.             appResources.allowFtp ? "True" : "False");
  354.     LOG (logFP, "\tpublicMode:\t%s\n",
  355.             appResources.publicMode ? "True" : "False");
  356.     LOG (logFP, "\tstatusWindow:\t%s\n",
  357.             appResources.statusWindow ? "True" : "False");
  358.     LOG (logFP, "\timageServers:\t%s\n",
  359.             appResources.imageServers);
  360.     LOG (logFP, "\tsoundServers:\t%s\n",
  361.             appResources.soundServers);
  362.     LOG (logFP, "\ttextServers:\t%s\n",
  363.             appResources.textServers);
  364.     LOG (logFP, "\tdirHistory:\t%s\n",
  365.             appResources.dirHistory ? "True" : "False");
  366.     LOG (logFP, "\tnameText:\t%s\n",
  367.             appResources.nameText ? "True" : "False");
  368.     LOG (logFP, "\tcommonText:\t%s\n",
  369.             appResources.commonText ? "True" : "False");
  370.     LOG (logFP, "\tconcurrentText:\t%d\n",
  371.              appResources.concurrentText);
  372.     LOG (logFP, "\tallowHold:\t%s\n",
  373.             appResources.allowHold ? "True" : "False");
  374.     LOG (logFP, "\tresetOptions:\t%s\n",
  375.             appResources.resetOptions ? "True" : "False");
  376.     LOG (logFP, "\titemStart:\t%d\n",
  377.              appResources.itemStart);
  378.     LOG (logFP, "\titemIncrement:\t%d\n",
  379.              appResources.itemIncrement);
  380.     LOG (logFP, "\tdirStart:\t%d\n",
  381.              appResources.dirStart);
  382.     LOG (logFP, "\tdirIncrement:\t%d\n",
  383.              appResources.dirIncrement);
  384.     LOG (logFP, "\twarpCursor:\t%s\n",
  385.             appResources.warpCursor ? "True" : "False");
  386.     LOG (logFP, "\tdoubleClick:\t%s\n",
  387.             appResources.doubleClick ? "True" : "False");
  388.     LOG (logFP, "\tmarkRoot:\t%s\n",
  389.             appResources.markRoot ? "True" : "False");
  390.     LOG (logFP, "\textendedTypes:\t%s\n",
  391.              appResources.extendedTypes);
  392.     LOG (logFP, "\ttempDirectory:\t%s\n",
  393.              appResources.tempDirectory);
  394.     LOG (logFP, "\tlogFile:\t%s\n",
  395.              appResources.logFile==NULL ? 
  396.                     "<none>" : appResources.logFile);
  397.     LOG (logFP, "\tprefixFile:\t%.*s\n",
  398.              PREFIX_LEN, appResources.prefixFile);
  399.     LOG (logFP, "\tprefixDir:\t%.*s\n",
  400.              PREFIX_LEN, appResources.prefixDir);
  401.     LOG (logFP, "\tprefixTelnet:\t%.*s\n",
  402.              PREFIX_LEN, appResources.prefixTelnet);
  403.     LOG (logFP, "\tprefixTn3270:\t%.*s\n",
  404.              PREFIX_LEN, appResources.prefixTn3270);
  405.     LOG (logFP, "\tprefixCSO:\t%.*s\n",
  406.              PREFIX_LEN, appResources.prefixCSO);
  407.     LOG (logFP, "\tprefixIndex:\t%.*s\n",
  408.              PREFIX_LEN, appResources.prefixIndex);
  409.     LOG (logFP, "\tprefixBinary:\t%.*s\n",
  410.              PREFIX_LEN, appResources.prefixBinary);
  411.     LOG (logFP, "\tprefixImage:\t%.*s\n",
  412.              PREFIX_LEN, appResources.prefixImage);
  413.     LOG (logFP, "\tprefixSound:\t%.*s\n",
  414.              PREFIX_LEN, appResources.prefixSound);
  415.     LOG (logFP, "\tprefixUnknown:\t%.*s\n",
  416.              PREFIX_LEN, appResources.prefixUnknown);
  417. #endif /* DEBUG */
  418.  
  419.     return &appResources;
  420. }
  421.